package iosr11.ui.data;

import iosr11.Service.ServiceLocatorImpl;
import iosr11.Service.EventService;
import iosr11.Service.AttendenceService;
import iosr11.Service.ProjectGroupService;
import iosr11.data.ProjectGroup;
import iosr11.data.Event;
import iosr11.data.EventSeries;
import iosr11.data.Attendence;

import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;

public class EventCurrentBean {
	private EventService service;
	private AttendenceService attendence_service;
	private ProjectGroupService group_service;
	private Logger logger = Logger.getLogger("iosr.ui.data");
	private Event event;
	private HtmlDataTable dataTable, dataTable2;
	
	public EventCurrentBean(){
		this.service = (EventService)ServiceLocatorImpl.getInstance().getService(Event.class);
		if(this.service == null){
			logger.severe("Could not obtain reference to EventService");
			return;
		}
		this.event = service.getCurrentEvent();
		if(this.event==null){
			logger.severe("Could not obtain student from EventService");
		}
	}
	
	public HtmlDataTable getDataTable(){
		return this.dataTable;
	}
	public void setDataTable(HtmlDataTable table){
		this.dataTable = table;
	}
	
	public HtmlDataTable getDataTable2(){
		return this.dataTable2;
	}
	public void setDataTable2(HtmlDataTable table){
		this.dataTable2 = table;
	}
	
	public boolean getEditMode(){
		return (this.event.getId()!=null);
	}
	
	public String goToGroupMarks(){
		ProjectGroup selectedGroup = (ProjectGroup) dataTable2.getRowData();
		Map sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		sessionMap.put(SessionMapKeys.SelectedGroupName.name(), selectedGroup.getGroupName());
		sessionMap.put(SessionMapKeys.SelectedEventID.name(), this.event.getId());
		return "goToGroupMarks";
	}
	
	public String updateAttendence(){
		List<Attendence> attendences = (List<Attendence>)this.dataTable.getValue();
		if(attendences == null){
			return "failure";
		}
		
		this.attendence_service = (AttendenceService)ServiceLocatorImpl.getInstance().getService(Attendence.class);
		if(this.attendence_service == null){
			logger.severe("Could not obtain reference to AttendenceService");
			return "failure";
		}
		
		boolean isSuccess=true;
		for (Attendence attendence : attendences) {
			if(!this.attendence_service.updateAttendence(attendence)){
				isSuccess=false;
			}
		}
		
		if(isSuccess){
			return "success";
		}else{
			return "failure";
		}
	}

	public Event getEvent(){
		return this.event;
	}
	
	public List<ProjectGroup> getGroups(){
		List<ProjectGroup> groupList;
		this.group_service = (ProjectGroupService)ServiceLocatorImpl.getInstance().getService(ProjectGroup.class);
		if(this.group_service == null){
			logger.severe("Could not obtain reference to groupService");
			return null;
		}
		groupList = group_service.getEventGroup(this.event);
		if(groupList==null){
			logger.severe("Could not obtain student from EventService");
			return null;
		}
		return groupList;
	}
}
